package org.example.exception.throwtype;



import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.example.exception.ResultCode;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * 自定义异常类型
 * @author yangzhenyu
 * @version 1.0
 * @create 2018-09-14 17:28
 **/
public class RunException extends RuntimeException implements Serializable {

    //错误代码
    ResultCode resultCode;

    public RunException(ResultCode resultCode){
        this.resultCode = resultCode;
    }

    public RunException(ResultCode resultCode,String desc){
        if (StringUtils.isNotEmpty(desc)){
            String oldDesc = resultCode.desc();
            if (oldDesc.contains("{0}")){
                resultCode.setDesc(MessageFormat.format(oldDesc, desc));
            }
        }
        this.resultCode = resultCode;
    }


    public RunException(ResultCode resultCode,String ... desc){
        if ( ObjectUtil.isNotNull(desc) && desc.length>0){
            String oldDesc = resultCode.desc();
            if (oldDesc.contains("{0}")){
                resultCode.setDesc(MessageFormat.format(oldDesc, desc));
            }
        }
        this.resultCode = resultCode;
    }


    public ResultCode getResultCode(){
        return resultCode;
    }


}
